<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后状态" prop="serviceStatus">
        <el-select v-model="queryParams.serviceStatus" placeholder="请选择订单售后状态" clearable size="small">
          <el-option
            v-for="dict in serviceStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item  label="申请时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="售后类型" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择售后类型" clearable size="small">
          <el-option
            v-for="dict in serviceTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderDetailList" >
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="售后状态" align="center" prop="serviceStatus">
        <template slot-scope="scope">
          <dict-tag :options="serviceStatusOptions" :value="scope.row.serviceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
      </el-table-column>
      <el-table-column label="售后类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="serviceTypeOptions" :value="scope.row.serviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="售后数量" align="center" prop="serviceQuantity" />
      <el-table-column label="售后金额" align="center" prop="serviceAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:orderDetail:edit']"
          >
            {{ scope.row.serviceStatus == "1"||scope.row.serviceStatus == "2"?"售后审核":"查看详情" }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 售后审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <div v-if="open">
        <el-row style="margin: 5px 0">
          <el-col :span="4" >订单编号:</el-col>
          <el-col :span="20">{{ form.orderCode }}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4" >收货人:</el-col>
          <el-col :span="20">{{ form.order.receiver }}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">联系电话:</el-col>
          <el-col :span="20">{{ form.order.phone }}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">收货地址:</el-col>
          <el-col :span="20">{{form.order.address?addressFormat(form.order.address):''}}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="4">申请诉求:</el-col>
          <el-col :span="20">
            <dict-tag :options="serviceTypeOptions" :value="form.serviceType"/>
          </el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">售后商品:</el-col>
          <el-col :span="20">
            <div  >
              <div style="display: inline-block;width: 80px;height: 80px"  >
                <el-image :src="baseUrl+form.picturePath"></el-image>
              </div>
              <div style="display: inline-block;margin-left: 15px;vertical-align: top;width: 280px;">
                <div style="text-align: left">{{form.goodsCode}}</div>
                <div style="text-align: left">{{form.goodsName}}</div>
                <div style="text-align: left">{{unitFormat(form.goodsUnit)}}</div>
              </div>
              <div style="display: inline-block;margin-left: 15px;vertical-align: top;;width: 70px;">
                <div style="text-align: right">×{{ form.serviceQuantity }}</div>
                <div style="text-align: right">￥{{form.price}}</div>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row style="margin: 5px 0;color: red">
          <el-col :span="4">申请描述:</el-col>
          <el-col :span="20" >{{form.serviceRemarks}}</el-col>
        </el-row>
        <el-row v-if="form.servicePicture" style="margin: 5px 0">
          <el-col :span="4">商品图片</el-col>
          <el-col :span="20" >
            <div v-for="pic in form.servicePicture.split(',')" style="width: 100px;height:100px;display: inline-block;">
              <img style="width: 100%;height: 100%" :src="baseUrl+pic">
            </div>
          </el-col>
        </el-row>
        <el-row v-if="form.order.deliveryMethod == '0'" style="margin: 5px 0">
          <el-col :span="4">快递公司:</el-col>
          <el-col :span="20" >{{form.order.expressCompany}}</el-col>
        </el-row>
        <el-row v-if="form.order.deliveryMethod == '0'" style="margin: 5px 0">
          <el-col :span="4">快递单号:</el-col>
          <el-col :span="20" >{{form.order.expressNumber}}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">下单时间:</el-col>
          <el-col :span="20" >{{form.order.createTime}}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">付款时间:</el-col>
          <el-col :span="20" >{{form.order.payTime}}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">发货时间:</el-col>
          <el-col :span="20" >{{form.order.deliveryTime}}</el-col>
        </el-row>
        <el-row style="margin: 5px 0">
          <el-col :span="4">完成时间:</el-col>
          <el-col :span="20" >{{form.order.finishTime}}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="4">售后状态:</el-col>
          <el-col :span="20">
            <dict-tag :options="serviceStatusOptions" :value="form.serviceStatus"/>
          </el-col>
        </el-row>
        <el-row  v-if="form.expressCompany" style="margin: 5px 0">
          <el-col :span="4">售后快递公司:</el-col>
          <el-col :span="20" >{{form.expressCompany}}</el-col>
        </el-row>
        <el-row v-if="form.expressNumber" style="margin: 5px 0">
          <el-col :span="4">售后快递单号:</el-col>
          <el-col :span="20" >{{form.expressNumber}}</el-col>
        </el-row>
        <el-row v-if="form.refuseDesc" style="margin: 5px 0">
          <el-col :span="4">商家驳回原因:</el-col>
          <el-col :span="20" >{{form.refuseDesc}}</el-col>
        </el-row>
      </div>
      <div v-if="service">
        <el-form  ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item  label="售后审核"  prop="expressCompany">
            <el-radio-group v-model="serviceStatus">
              <el-radio v-if="form.serviceStatus =='1'"   label="2" >同意</el-radio>
              <el-radio v-if="form.serviceStatus =='1'"  label="3" >驳回</el-radio>
              <el-radio v-if="form.serviceStatus =='2'"  label="4" >完成</el-radio>
              <el-radio   label="5" >关闭</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="serviceStatus =='3'"  label="驳回原因"  prop="refuseDesc">
            <el-input v-model="form.refuseDesc" type="textarea"  placeholder="请输入驳回原因"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button v-if="service" type="primary" @click="submitForm">确定审核</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderDetail, getOrderDetail, updateOrderDetail } from "@/api/order/orderDetail";

export default {
  name: "OrderDetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      baseUrl:process.env.VUE_APP_BASE_API,
      // 订单详情表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange:[],
      // 订单售后状态：0无售后，1待审核，2审核通过字典
      serviceStatusOptions: [],
      service:true,
      serviceStatus: "2",
      // 售后类型：0退货 1换货字典
      serviceTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        serviceStatus: null,
        applicationTime: null,
        serviceType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDicts("service_status").then(response => {
      this.serviceStatusOptions = response.data;
    });
    this.getDicts("service_type").then(response => {
      this.serviceTypeOptions = response.data;
    });

  },
  methods: {
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      listOrderDetail(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderDetailList = response.rows;
        console.log(this.orderDetailList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        extAppid: null,
        orderCode: null,
        goodsId: null,
        goodsUnit: null,
        quantity: null,
        price: null,
        amount: null,
        scoringTime: null,
        score: null,
        comment: null,
        reply: null,
        serviceStatus: "0",
        applicationTime: null,
        serviceType: null,
        serviceQuantity: null,
        serviceAmount: null,
        serviceRemarks: null,
        servicePicture: null,
        expressCompany:null,
        expressNumber:null,
        refuseDesc:null
      };
      this.service = false;
      this.serviceStatus = '2';
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange=[];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getOrderDetail(id).then(response => {
        this.form = response.data;
        if(this.form.serviceStatus == '1'||this.form.serviceStatus == '2'){
          this.service = true;
        }
        this.open = true;
        this.title = "售后订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.serviceStatus = this.serviceStatus;
            updateOrderDetail(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    addressFormat(address){
      address=address.replaceAll('/','');
      address=address.replaceAll('-','');
      return address
    },
    unitFormat(obj){
      let str = ''
      if(obj){
        let a = Object.values(JSON.parse(obj));
        a.forEach((value,index) => {
          if (index == 0){
            str += value;
          }else {
            str+="/"+value;
          }
        })
      }
      return str;
    },
  }
};
</script>
